using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

namespace MobileEditor
{
	/// <summary>
	/// A base class used for XML persisted configuration settings.
	/// </summary>
	/// <remarks>
	/// <para>Authors</para>
	/// <ul>
	/// <li>KEB - Karl E Bradshaw</li>
	/// </ul>
	/// <para>Version History</para>
	/// <table border="1">
	/// <tr><td>Date</td><td>Author</td><td>Change No</td><td>Description</td></tr>
	/// <tr><td>2006-12-15</td><td>KEB</td><td></td><td>Initial version.</td></tr>
	/// </table>
	/// </remarks>
	abstract public class SettingsBase
	{
		/// <summary>
		/// Generates the fully qualified path of the config file.
		/// </summary>
		/// <returns>The full path of the config file.</returns>
		public static string GeneratePath()
		{
			return GeneratePath(Assembly.GetExecutingAssembly().GetName().Name + ".config");
		}

		/// <summary>
		/// Generates the fully qualified path of the config file with the specified name.
		/// </summary>
		/// <param name="filename">The name of the config file.</param>
		/// <returns>The full path of the config file.</returns>
		public static string GeneratePath(string filename)
		{
			string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
			return Path.Combine(appPath, filename);
		}

		/// <summary>
		/// Deserializes the config class from the file.
		/// </summary>
		/// <typeparam name="T">The type of the config class.</typeparam>
		/// <returns>The deserialized object.</returns>
		public static T Load<T>()
			where T : new()
		{
			return Load<T>(GeneratePath());
		}

		/// <summary>
		/// Deserializes the config class from the file.
		/// </summary>
		/// <typeparam name="T">The type of the config class.</typeparam>
		/// <param name="filename">The name of the file to deserialize the class from.</param>
		/// <returns>The deserialized object.</returns>
		public static T Load<T>(string filename)
			where T: new()
		{
			T settings;
			if (File.Exists(filename))
			{
				XmlSerializer s = new XmlSerializer(typeof(T));
				using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
				{
					settings = (T)s.Deserialize(fs);
				}
			}
			else settings = new T();
			return settings;
		}
		

		/// <summary>
		/// Initializes a new instance of the SettingsBase class.
		/// </summary>
		protected SettingsBase()
		{
		}


		/// <summary>
		/// Serializes the settings class into a file.
		/// </summary>
		public void Save()
		{
			Save(GeneratePath());
		}

		/// <summary>
		/// Serializes the settings class into the specified file.
		/// </summary>
		/// <param name="filename">The name of the file to serialize the object into.</param>
		public void Save(string filename)
		{
			//System.Type type = GetType();
			XmlSerializer s = new XmlSerializer(GetType());
			using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
			{
				s.Serialize(fs, this);
			}
		}
	}
}
